<?php

namespace App\Libs;

use App\Menu;

class TreeMenu
{
    protected $tree;
    
    public function __construct()
    {
        $this->tree = $this->generateTreeMenu();
    }

    public function generateTreeMenu( $pid = 0)
    {   
        // 暂时只有2级菜单， 等业务扩展再重写此处
        $menu = Menu::orderBy('parent_id', 'Asc')->get()->toArray();
        $tree = [];
        foreach ($menu as $key => $value) {
           if(isset($tree[$value['parent_id']])) {
                $tree[$value['parent_id']]['child'][$value['menu_id']] = [
                    'node' => $value
                ];
           } else {
                $tree[$value['menu_id']]['node'] = $value;
           }
        }
        return $tree;
    }

    public function show()
    {
        view()->share(['treeMenu' => $this->tree]);
    }

}
